package com.example.ass7;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.List;

import biuoop.GUI;
import biuoop.KeyboardSensor;
import biuoop.Sleeper;

import com.example.ass7.animation.AnimationRunner;
import com.example.ass7.animation.HighScoresAnimation;
import com.example.ass7.animation.KeyPressStoppableAnimation;
import com.example.ass7.animation.Menu;
import com.example.ass7.animation.MenuAnimation;
import com.example.ass7.dataloader.LevelSpecificationReader;
import com.example.ass7.level.LevelInformation;
import com.example.ass7.task.QuitGameTask;
import com.example.ass7.task.ShowHiScoresTask;
import com.example.ass7.task.StartGameTask;
import com.example.ass7.task.Task;

/**
 * @author Meirav Ben Izhak
 */
public class Ass7Game {

    public static final String FILE_NAME = "highscores.txt";
    public static final String LEVEL_SETS_FILE_NAME = "level_sets.txt";
    public static final String LEVEL_DEFINITIONS =
            "/definitions/level_definitions.txt";

    /**
     * @param args
     *            args
     * @throws IOException
     *             IOException
     */
    public static void main(String[] args) throws IOException {

        GUI gui = new GUI("Space Invaders", 800, 600);
        Sleeper sleeper = new Sleeper();
        int frameRate = 60;
        AnimationRunner animationRunner = new AnimationRunner(sleeper, gui,
                frameRate);

        File file = new File(FILE_NAME);
        if (!file.exists()) {
            file.createNewFile();
        }
        final HighScoresTable highScoresTable = HighScoresTable
                .loadFromFile(file);
        HighScoresAnimation highScoresAnimation = new HighScoresAnimation(
                highScoresTable);

        KeyPressStoppableAnimation highScoresAnimationWithStoppable =
                new KeyPressStoppableAnimation(
                gui.getKeyboardSensor(), KeyboardSensor.SPACE_KEY,
                highScoresAnimation);


        InputStream inputStream =
                LevelSpecificationReader.class
                .getResourceAsStream(LEVEL_DEFINITIONS);
        LevelSpecificationReader levelSpecificationReader =
                new LevelSpecificationReader();
        Reader levelSpecificationFileReader =
                new InputStreamReader(inputStream);
        List<LevelInformation> orders = levelSpecificationReader
                .fromReader(levelSpecificationFileReader);


        Menu<Task<Void>> menu = new MenuAnimation<Task<Void>>(
                gui.getKeyboardSensor(), animationRunner);
        menu.addSelection("s",
                "Start Game",
                new StartGameTask(
                        gui.getKeyboardSensor(),
                        highScoresTable,
                        animationRunner,
                        orders));
        menu.addSelection("h",
                "High scores",
                new ShowHiScoresTask(animationRunner,
                        highScoresAnimationWithStoppable));
        menu.addSelection("e",
                "Exit",
                new QuitGameTask());


        while (true) {
            animationRunner.run(menu);
            // wait for user selection
            Task<Void> task = menu.getStatus();
            task.run();
        }
        // gui.close();
    }
}
